﻿using ControllerClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NewCash2DeviceServer
{
 
public class KCRXMAC
    {
        static LogHelperNew log = new LogHelperNew();
        public static ulong GetMAC(byte[] data, byte[] key)
        {
            Console.WriteLine("data" + data.Length);
            String slshuju = HexUtils.byteToHexStr(data);
            //data必须是8字节的整数倍，不足低位补0
            byte[] dataAll = HexUtils.AddZero(slshuju);
            Console.WriteLine("len"+ dataAll.Length);
            ICryptoTransform transform = new DESCryptoServiceProvider
            {
                Mode = CipherMode.CBC,
                Padding = PaddingMode.None
            }.CreateEncryptor(key, new byte[8]);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            cryptoStream.Write(dataAll, 0, dataAll.Length);
            byte[] array = new byte[8];
            array = memoryStream.ToArray();
            cryptoStream.FlushFinalBlock();
            cryptoStream.Close();
            byte[] value = memoryStream.ToArray();
            Console.WriteLine("value" + value.Length+"=="+HexUtils.byteToHexStr(value));
            byte[] processKey = new byte[4];
            memoryStream.Close();
            return BitConverter.ToUInt64(value, value.Length-8);
        }


        public static byte[] GetMACTest(byte[] data, byte[] key)
        {
            Console.WriteLine("data" + data.Length);
            String slshuju = HexUtils.byteToHexStr(data);
            //data必须是8字节的整数倍，不足低位补0
            byte[] dataAll = HexUtils.AddZero(slshuju);
            Console.WriteLine("len" + dataAll.Length);
            ICryptoTransform transform = new DESCryptoServiceProvider
            {
                Mode = CipherMode.CBC,
                Padding = PaddingMode.None
            }.CreateEncryptor(key, new byte[8]);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            cryptoStream.Write(dataAll, 0, dataAll.Length);
            byte[] array = new byte[8];
            array = memoryStream.ToArray();
            cryptoStream.FlushFinalBlock();
            cryptoStream.Close();
            byte[] value = memoryStream.ToArray();
            Console.WriteLine("value" + value.Length + "==" + HexUtils.byteToHexStr(value));
            byte[] mac = new byte[4];
            Array.Copy(value, value.Length - 8, mac, 0, 4);
            Console.WriteLine("mac" + HexUtils.byteToHexStr(mac));
            memoryStream.Close();
            log.WriteLine("mac" + HexUtils.byteToHexStr(mac));
            return mac;
        }

        public static byte[] SIDBuild(byte[] rand1, byte[] rand2, uint ControllerId)
        {
            byte[] rand3 = new byte[4];
            rand3 = HexUtils.xOr(rand1, rand2);
            byte cid = BitConverter.GetBytes(ControllerId)[0]; ;

            byte[] sid = new byte[]
            {

                rand3[0],
                rand3[1],
                rand3[2],
                cid
            };
            Console.WriteLine("sid" + HexUtils.byteToHexStr(sid));
            log.WriteLine("sid" + HexUtils.byteToHexStr(sid));

            return sid;
        }

        public static byte[] ProcessBuild(byte[] data, byte[] key)
        {

            Console.WriteLine("data" + data.Length);
            String slshuju = HexUtils.byteToHexStr(data);
            //data必须是8字节的整数倍，不足低位补0
            byte[] dataAll = HexUtils.AddZero(slshuju);
            Console.WriteLine("len" + dataAll.Length);
            ICryptoTransform transform = new DESCryptoServiceProvider
            {
                Mode = CipherMode.CBC,
                Padding = PaddingMode.None
            }.CreateEncryptor(key, new byte[8]);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
            cryptoStream.Write(dataAll, 0, dataAll.Length);
            byte[] array = new byte[8];
            array = memoryStream.ToArray();
            cryptoStream.FlushFinalBlock();
            cryptoStream.Close();
            byte[] value = memoryStream.ToArray();
            Console.WriteLine("value" + value.Length + "==" + HexUtils.byteToHexStr(value));
            memoryStream.Close();
            log.WriteLine("processKey" + HexUtils.byteToHexStr(value));
            return value;

        }
    }

}
